package com.cy.controller;

import com.cy.bfzcommons.pojo.Courier;
import com.cy.bfzcommons.service.CourierService;
import com.cy.bfzcommons.web.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/courier")
@Api(
        value = "/courier",description = "CourierController"
        ,tags = "地址模块"
)
public class CourierController {
    @Autowired
    private CourierService courierService;

    //http://localhost/courier?userId=1
    //http://localhost/courier/1/32/32
    //根据user_id查询用户有多少个地址
    @GetMapping("/{userId}")
    public JsonResult getCourier(@ApiParam(name="userId",value="用户id",required=true)@PathVariable Integer userId){
        List<Courier> courierList=courierService.getCourier(userId);
        return JsonResult.ok(courierList);
    }

    //增加地址
    //@RequestBody JSON字符串转换为对象
    @PostMapping
    public JsonResult addCourier(@ApiParam(name="courier",value="地址信息",required=true) @RequestBody Courier courier){
        courierService.addCourier(courier);
        return JsonResult.ok();
    }


    //删除地址
    @DeleteMapping("/{courier_id}")
    public JsonResult deleteCourier(@ApiParam(name="courier_id",value="地址id",required=true)@PathVariable Integer courier_id){
        courierService.deleteCourier(courier_id);
        return JsonResult.ok();
    }


    //更改地址
    @PutMapping
    public JsonResult updateCourier(@ApiParam(name="courier",value="地址信息",required=true)@RequestBody Courier courier){
        courierService.updateCourier(courier);
        return JsonResult.ok();
    }



}
